﻿function iskyygcbenLoad() {
    debugger;
    DisabledYJLR();
    var productenname = GetProductInfo();
    Xrm.Page.getAttribute("new_chanpinyingwen").setValue(productenname);
    
    Xrm.Page.data.save();
}

Xrm.Page.getAttribute("new_chanpin").addOnChange(function () {
    debugger;
    var productenname = GetProductInfo();
    Xrm.Page.getAttribute("new_chanpinyingwen").setValue(productenname);
    Xrm.Page.data.save();
});

Xrm.Page.getAttribute("new_case").addOnChange(function () {
    debugger;
    DisabledYJLR();
    Xrm.Page.data.save();
});


function DisabledYJLR() {
    var caseId = Xrm.Page.getAttribute("new_case").getValue();
    if (caseId != null) {
        var orderresult = GetOrderInfo(caseId[0].id);
        if (orderresult != null) {
            if (orderresult[0].attributes.new_status.value == 100000001) {
                DisableForm(true);
            }
        }
    }
}


function GetProductInfo() {
    var productId = Xrm.Page.getAttribute("new_chanpin").getValue();//预计重量
    if (productId != null) {
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
            "  <entity name='product'>" +
            "   <attribute name='name' />" +
            "   <attribute name='new_enname' />" +
            "   <order attribute='name' descending='false' />" +
            "   <filter type='and'>" +
            "     <condition attribute='productid' operator='eq'  value='" + productId[0].id.replace("{", "").replace("}", "") + "' />" +
            "   </filter>" +
            "  </entity>" +
            " </fetch>";
        var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
        if (retrTotalQuota && retrTotalQuota.length > 0 && retrTotalQuota[0].attributes.new_enname != undefined) {
            return retrTotalQuota[0].attributes.new_enname.value;
        }
    }
    return null;
}



//根据订单查询信息
function GetOrderInfo(orderId) {
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
        " <entity name = 'salesorder'>" +
        "     <attribute name='name'/>" +
        "     <attribute name='new_status'/>" +
        "     <order attribute='name' descending='false'/>" +
        "     <filter type='and'>" +
        "         <condition attribute='salesorderid' operator='eq' value='"+orderId+"' />" +
        "   </filter>" +
        "  </entity>" +
        " </fetch>";
    var retrTotalQuota = XrmServiceToolkit.Soap.Fetch(fetchXml);
    if (retrTotalQuota && retrTotalQuota.length > 0 && retrTotalQuota[0].attributes.new_status != undefined) {
        return retrTotalQuota
    }
    return null;
}

//显示隐藏整个窗体的字段
function DisableForm(bool) {
    var controls = Xrm.Page.ui.controls.get();
    for (var i in controls) {
        var control = controls[i];
        if (control.getControlType() != "iframe" && control.getControlType() != "webresource"
            && control.getControlType() != "subgrid") {
            control.setDisabled(bool);
        }
    }
}


//预估成本重传按钮
function ShowYGCBRetransmitButton() {
    debugger;
    var showRoles = ["系统管理员", "系统定制员"];
    var risvisiable = false;
    var sisvisiable = false;
    var roleNames = XrmServiceToolkit.Soap.GetCurrentUserRoles();
    if (roleNames && roleNames.length > 0) {
        for (var i = 0; i < roleNames.length; i++) {
            for (var j = 0; j < showRoles.length; j++) {
                if (roleNames[i] == showRoles[j]) {
                    risvisiable = risvisiable || true;
                    break;
                } else {
                    risvisiable = risvisiable || false;
                    continue;
                }
            }
            if (risvisiable) break;
        }
    } else {
        risvisiable = risvisiable || false;
    }

    var caseId = Xrm.Page.getAttribute("new_case").getValue();
    if (caseId != null) {
        var orderresult = GetOrderInfo(caseId[0].id);
        if (orderresult != null) {
            if (orderresult[0].attributes.new_status.value == 100000001) {
                sisvisiable = sisvisiable || true;
            }
        }
    }
    debugger;
    return (sisvisiable && risvisiable);
}

